package com.hivekion.system.controller;

import com.hivekion.common.annotation.AutoLog;
import com.hivekion.common.entity.TreeNode;
import com.hivekion.common.enums.OperationTypeEnum;
import com.hivekion.common.exception.BusinessException;
import com.hivekion.system.domain.SysStatisticsItem;
import com.hivekion.system.domain.vo.SysStatisticsItemCreateInputVo;
import com.hivekion.system.domain.vo.SysStatisticsItemUpdateInputVo;
import com.hivekion.system.domain.vo.SysStatisticsItemViewVo;
import com.hivekion.system.service.ISysStatisticsItemService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/statisticsItem")
public class SysStatisticsItemController {
      @Autowired
      ISysStatisticsItemService statisticsItemService;

      @GetMapping("/getTreeSelect")
      @AutoLog(value = "获取统计项树结构", operationType = OperationTypeEnum.SELECT, module = "系统管理/统计项管理")
      public List<TreeNode> getTreeSelect() {
	    return statisticsItemService.getTreeSelect();
      }

      @GetMapping("/getList")
      @AutoLog(value = "获取统计项列表", operationType = OperationTypeEnum.SELECT, module = "系统管理/统计项管理")
      public List<SysStatisticsItemViewVo> getList() {
	    return statisticsItemService.getList();
      }

      @PostMapping("/add")
      @PreAuthorize("@Permission.hasPermi('statistics_item:add')")
      @AutoLog(value = "新增统计项", operationType = OperationTypeEnum.INSERT, module = "系统管理/统计项管理")
      public boolean add(@RequestBody SysStatisticsItemCreateInputVo inputVo) {
	    if (statisticsItemService.checkItemNameUnique(inputVo.getName(), "")) {
		  throw new BusinessException(500, "统计项名称重复");
	    }
	    return statisticsItemService.create(inputVo);
      }

      @ApiOperation(value = "编辑", notes = "编辑统计项")
      @PutMapping("/edit")
      @PreAuthorize("@Permission.hasPermi('statistics_item:edit')")
      @AutoLog(value = "更改统计项", operationType = OperationTypeEnum.UPDATE, module = "系统管理/编辑统计项")
      public boolean update(@RequestBody SysStatisticsItemUpdateInputVo inputVo) {
	    if (inputVo.getId().equals(inputVo.getParentId())) {
		  throw new BusinessException(500, "禁止选择统计项为父级统计项");
	    }
	    if (statisticsItemService.checkItemNameUnique(inputVo.getName(), inputVo.getId())) {
		  throw new BusinessException(500, "统计项名称已存在");
	    }
	    return statisticsItemService.update(inputVo);
      }

      @GetMapping("/getInfo")
      @AutoLog(value = "获取单个统计项信息", operationType = OperationTypeEnum.SELECT, module = "系统管理/统计项管理")
      public SysStatisticsItem getInfo(String itemId) {
	    return statisticsItemService.getInfo(itemId);
      }

      ;

      @PostMapping("/checkNameUnique")
      public boolean checkNameUnique(String itemName, String itemId) {
	    return statisticsItemService.checkItemNameUnique(itemName, itemId);
      }

      @DeleteMapping("/remove")
      @PreAuthorize("@Permission.hasPermi('statistics_item:delete')")
      @AutoLog(value = "删除统计项", operationType = OperationTypeEnum.DELETE, module = "系统管理/统计项管理")
      public boolean delete(String itemId) {
	    // TODO 需要删除 统计项和子项的所有数据
	    return statisticsItemService.delete(itemId);
      }
}
